<?php

namespace App\Org\Developer;

/**
 * @author    陈玉琦
 */
class Helper
{
    /**
     * 对 id 类的数字进行验证
     *
     * @param int|string $id 某个 xxx_id
     * @param int $val $id的最小值
     * @return bool    为空、不是数字、含有点、还有e、小于指定值等，都返回true，表示无效。
     */
    public static function idInvalid($id = 0, $val = 0)
    {
        return (empty($id) || !is_numeric($id) || (strpos($id, '.') !== false) || (strpos($id, 'e') !== false) || (intval($id) <= $val));
    }

    /**
     * 判断操作的资源是否是用户自己的
     *
     * @param int|string $user_id 当前登录用户的 user_id
     * @param int|string $resource_user_id 操作资源所属的 user_id
     * @return boolean
     */
    public static function isUserSelfResource($user_id = 0, $resource_user_id = 0)
    {
        if (is_null($resource_user_id)) {
            return false;
        }
        return $resource_user_id == $user_id;
    }

    /**
     * 导出 Excel
     *
     * @param array $data 数据
     * @param array $title 标头
     * @param string $filename 文件名称
     * @param string $field 需要逐个字符转码的key
     */
    public static function downloadExcel($data = [], $title = [], $filename = '测试', $field = [])
    {
        // header("Content-type:application/octet-stream");// 两个Content-Type 任何一个都可以的；
        header("Accept-Ranges:bytes");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=".$filename.".xls");
        header("Pragma: no-cache");
        header("Expires: 0");

        //导出xls 开始
        if (!empty($title)){
            foreach ($title as $k => $v) {
                $title[$k] = iconv("UTF-8", "GB2312", $v);
            }
            $title = implode("\t", $title);
            echo "$title\n";
        }
        if (!empty($data)){
            foreach($data as $key => $val){
                foreach ($val as $ck => $cv) {
                    if (in_array($ck, $field)) {
                        // 增加对特殊符号的处理
                        $len = mb_strlen($cv, 'UTF-8');
                        $encodeStr = '';
                        for($i = 0; $i < $len; $i++) {
                            $char = mb_substr($cv, $i, 1, 'UTF-8');
                            $temp = @iconv('UTF-8', 'GB2312', $char);
                            if($temp === false) {
                                continue;
                            }
                            $encodeStr .= $temp;
                        }
                        $data[$key][$ck] = $encodeStr;
                    } else {
                        $data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
                    }
                }
                $data[$key] = implode("\t", $data[$key]);
            }
            // echo 'downloadExcel<br/>';
            // var_dump($data);exit;
            echo implode("\n", $data);
        }
    }

    /**
     * 生成二维码图片并返回路径
     */
    public static function makeQrcodeImage($url = '', $user_id = 0)
    {
        $urlPath = 'qrcodes/'. substr(md5($user_id), 5, 8);
        $absolutePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $urlPath;
        if (!file_exists($absolutePath)) {
            mkdir($absolutePath, 0777, true);
        }
        $urlPath .= '/'. time() . '' . mt_rand(1000, 9999) . '.png';
        Qrcode::png($url, $urlPath, 'L', 3); // 默认 99*99 px。参数4在 33*33 px 基础上进行乘积，比如，如果是10，生成的大小是 330*330 px。
        return 'http://theend.jumpcity.cn/' .$urlPath;
    }


    public static function log($data = '')
    {
        \Illuminate\Support\Facades\DB::insert("INSERT thearte_tests (title) values ('{$data}')");
    }

    public static function logss($data = '')
    {
        \Illuminate\Support\Facades\DB::insert("INSERT thearte_tests (content) values ('{$data}')");
    }
}